package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmLinkman;
import com.nuoniu.sibanyun.entity.crm.CrmLinkmanHistoryRecord;
import com.nuoniu.sibanyun.entity.crm.dto.NotTaskingLinkManSearchDto;
import com.nuoniu.sibanyun.entity.dto.CrmLinkmanDto;
import com.nuoniu.sibanyun.entity.dto.CrmLinkmanSaveDto;
import com.nuoniu.sibanyun.service.crm.ICrmLinkmanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 客户联系人 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-15
 */
@RestController
@RequestMapping("/crm/linkman")
@Api(value="联系人 ",tags={"联系人"})
public class CrmLinkmanController extends BaseController {

    @Autowired
    private ICrmLinkmanService linkmanService;

    @GetMapping("/list")
    @ApiOperation(value = "根据客户ID查询联系人",tags = "APP_联系人")
    public HttpResult<List<CrmLinkman>> list(Page page, CrmLinkmanDto crmLinkmanDto) {
        return HttpResult.ok(linkmanService.listByDto( page, crmLinkmanDto));
    }

    /**
     * 获取联系人记录
     * @author 小啊刚
     * @date 2020/6/17
     */
    @GetMapping("/detail")
    @ApiOperation(value = "联系人详情",tags = "APP_联系人")
    public HttpResult<CrmLinkman> detail(Integer id) {
        if(null == id || id < 1){
            throw new NuoNiuException("参数错误");
        }

        return HttpResult.ok(linkmanService.getById(id));
    }

    @PutMapping("setKP")
    @ApiOperation(value = "设置KP")
    public HttpResult setKP(Long linkManId,Long crmId) {
        linkmanService.setKP( linkManId, crmId);
        return HttpResult.ok();
    }

    /**
     * 保存联系人
     * @author 小啊刚
     * @date 2020/6/23
     */
    @PostMapping("save")
    @ApiOperation(value = "保存联系人",tags = "APP_联系人")
    public HttpResult save(@RequestBody CrmLinkmanSaveDto linkmanSaveDto) {
        if(null == linkmanSaveDto.getCrmId() || linkmanSaveDto.getCrmId() < 1){
            throw new NuoNiuException("参数错误");
        }

        linkmanService.saveLinkman(linkmanSaveDto);
        return HttpResult.ok();
    }

//    /**
//     * 删除联系人
//     * @author 小啊刚
//     * @date 2020/6/23
//     */
//    @DeleteMapping("delete")
//    public HttpResult delete(Integer id) {
//        linkmanService.removeById(id);
//        return HttpResult.ok();
//    }

    /**
     * 停用联系人
     */
    @GetMapping(value = "disable")
    @ApiOperation(value = "停用联系人",tags = "APP_联系人")
    public HttpResult disable(String ids) {
        if (StringUtils.isNoneBlank(ids)) {
           linkmanService.disable(ids);
        }else{
            throw new NuoNiuException("参数错误");
        }
        return HttpResult.ok("停用联系人成功");
    }

    /**
     * 启用联系人
     */
    @GetMapping(value = "enable")
    @ApiOperation(value = "启用联系人",tags = "APP_联系人")
    public HttpResult enable(String ids) {
        if (StringUtils.isNoneBlank(ids)) {
            for (String id : ids.split(",")) {
                CrmLinkman crmLinkman = new CrmLinkman();
                crmLinkman.setId(Long.valueOf(id));
                crmLinkman.setStatus(0);
                linkmanService.updateById(crmLinkman);
            }
        }
        return HttpResult.ok("启用联系人成功");
    }

    @ApiOperation("获取联系人历史操作记录")
    @GetMapping("pageHistoryList")
    public HttpResult<CrmLinkmanHistoryRecord> pageHistoryList(Page page, Integer crmId) {
        return HttpResult.ok(linkmanService.pageHistoryList(page, crmId));
    }

    @ApiOperation("查询 可以添加任务的 联系人")
    @PostMapping("pageNotTaskingLinkMan")
    public HttpResult<CrmLinkmanHistoryRecord> pageNotTaskingLinkMan(@RequestBody NotTaskingLinkManSearchDto notTaskingLinkManSearchDto) {
        return HttpResult.ok(linkmanService.pageNotTaskingLinkMan(notTaskingLinkManSearchDto));
    }
}
